<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 字符串划分（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>给定一个小写字母组成的字符串 s&#xff0c;请找出字符串中两个不同位置的字符作为分割点&#xff0c;使得字符串分成三个连续子串且子串权重相等&#xff0c;注意子串不包含分割点。</p> 
<p>若能找到满足条件的两个分割点&#xff0c;请输出这两个分割点在字符串中的位置下标&#xff0c;若不能找到满足条件的分割点请返回0,0。</p> 
<p>子串权重计算方式为:子串所有字符的ASCII码数值之和。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入为一个字符串&#xff0c;字符串由a~z&#xff0c;26个小写字母组成&#xff0c;5 ≤ 字符串长度 ≤ 200。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出为两个分割点在字符串中的位置下标&#xff0c;以逗号分隔</p> 
<p></p> 
<h4>备注</h4> 
<p>只考虑唯一解&#xff0c;不存在一个输入多种输出解的情况</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">acdbbbca</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">2,5</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">以位置2和5作为分割点&#xff0c;将字符串分割为ac&#xff0c;bb&#xff0c;ca三个子串&#xff0c;每一个的子串权重都为196&#xff0c;输出为&#xff1a;2,5</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">abcabc</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">0,0</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">找不到符合条件的分割点&#xff0c;输出为&#xff1a;0,0</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题数量级不大&#xff0c;可以考虑暴力求解。即定义两个指针i, j&#xff0c;去模拟两个分割点&#xff0c;其中 i &lt; j&#xff0c;且 j - i &gt; 1&#xff0c;因为被分割的子串至少有1个字母。</p> 
<p>另外&#xff0c;为了避免重复求解某范围内的子串权重&#xff0c;我们可以使用前缀和&#xff0c;实现O(1)时间计算出任意区间内的子串权重。</p> 
<p>关于前缀和知识&#xff0c;可以看&#xff1a;<a href="https://fcqian.blog.csdn.net/article/details/128976936" rel="nofollow" title="算法设计 - 前缀和 &amp; 差分数列_伏城之外的博客-CSDN博客">算法设计 - 前缀和 &amp; 差分数列_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">const rl &#61; require(&#34;readline&#34;).createInterface({ input: process.stdin });
var iter &#61; rl[Symbol.asyncIterator]();
const readline &#61; async () &#61;&gt; (await iter.next()).value;

void (async function () {
  const s &#61; await readline();
  const chars &#61; [...s].map((c) &#61;&gt; c.charCodeAt());
  const n &#61; chars.length;

  // 前缀和
  const preSum &#61; new Array(n &#43; 1).fill(0);
  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    preSum[i] &#61; preSum[i - 1] &#43; chars[i - 1];
  }

  // i,j是分割点
  for (let i &#61; 1; i &lt; n; i&#43;&#43;) {
    // sum1 是 0 ~ i-1 范围的ASCII码之和
    const sum1 &#61; preSum[i] - preSum[0];

    for (let j &#61; i &#43; 2; j &lt; n; j&#43;&#43;) {
      // sum2 是 i &#43; 1 ~ j - 1 范围的ASCII码之和
      const sum2 &#61; preSum[j] - preSum[i &#43; 1];

      // 剪枝
      if (sum1 &lt; sum2) break;

      if (sum1 &#61;&#61; sum2) {
        // sum3 是 j &#43; 1 ~ n - 1 范围的ASCII码之和
        const sum3 &#61; preSum[n] - preSum[j &#43; 1];

        if (sum2 &#61;&#61; sum3) {
          return console.log(&#96;${i},${j}&#96;);
        }
      }
    }
  }

  console.log(&#34;0,0&#34;);
})();
</code></pre> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);
    System.out.println(getResult(sc.nextLine()));
  }

  public static String getResult(String s) {
    char[] chars &#61; s.toCharArray();
    int n &#61; chars.length;

    // 前缀和
    int[] preSum &#61; new int[n &#43; 1];
    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      preSum[i] &#61; preSum[i - 1] &#43; chars[i - 1];
    }

    // i,j是分割点
    for (int i &#61; 1; i &lt; n; i&#43;&#43;) {
      // sum1 是 0 ~ i-1 范围的ASCII码之和
      int sum1 &#61; preSum[i] - preSum[0];
      for (int j &#61; i &#43; 2; j &lt; n; j&#43;&#43;) {
        // sum2 是 i &#43; 1 ~ j - 1 范围的ASCII码之和
        int sum2 &#61; preSum[j] - preSum[i &#43; 1];

        // 剪枝
        if (sum1 &lt; sum2) break;

        if (sum1 &#61;&#61; sum2) {
          // sum3 是 j &#43; 1 ~ n - 1 范围的ASCII码之和
          int sum3 &#61; preSum[n] - preSum[j &#43; 1];

          if (sum2 &#61;&#61; sum3) {
            return i &#43; &#34;,&#34; &#43; j;
          }
        }
      }
    }

    return &#34;0,0&#34;;
  }
}
</code></pre> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
s &#61; input()


# 算法入口
def getResult():
    chars &#61; list(map(ord, s))
    n &#61; len(chars)

    # 前缀和
    preSum &#61; [0] * (n&#43;1)
    for i in range(1, n &#43; 1):
        preSum[i] &#61; preSum[i - 1] &#43; chars[i - 1]

    # i,j是分割点
    for i in range(1, n):
        # sum1 是 0 ~ i-1 范围的ASCII码之和
        sum1 &#61; preSum[i] - preSum[0]

        for j in range(i &#43; 2, n):
            # sum2 是 i &#43; 1 ~ j - 1 范围的ASCII码之和
            sum2 &#61; preSum[j] - preSum[i &#43; 1]

            # 剪枝
            if sum1 &lt; sum2:
                break

            if sum1 &#61;&#61; sum2:
                # sum3 是 j &#43; 1 ~ n - 1 范围的ASCII码之和
                sum3 &#61; preSum[n] - preSum[j &#43; 1]

                if sum2 &#61;&#61; sum3:
                    return f&#34;{i},{j}&#34;

    return &#34;0,0&#34;


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 200

int main()
{
	char str[MAX_SIZE];
	scanf(&#34;%s&#34;, str);
	
	int n &#61; strlen(str);
	
	// 前缀和
	int preSum[MAX_SIZE &#43; 1] &#61; {0};
	for(int i&#61;1; i&lt;&#61;n; i&#43;&#43;) {
		preSum[i] &#61; preSum[i-1] &#43; str[i-1];
	}
	
	// i,j是分割点
	for(int i&#61;1; i&lt;n; i&#43;&#43;) {
		// sum1 是 0 ~ i-1 范围的ASCII码之和
		int sum1 &#61; preSum[i] - preSum[0];
		
		for(int j&#61;i&#43;2; j&lt;n; j&#43;&#43;) {
			// sum2 是 i &#43; 1 ~ j - 1 范围的ASCII码之和
			int sum2 &#61; preSum[j] - preSum[i&#43;1];
			
			// 剪枝
			if(sum1 &lt; sum2) break;
			
			if(sum1 &#61;&#61; sum2) {
				// sum3 是 j &#43; 1 ~ n - 1 范围的ASCII码之和
				int sum3 &#61; preSum[n] - preSum[j&#43;1];
				
				if(sum2 &#61;&#61; sum3) {
					printf(&#34;%d,%d&#34;, i, j);
					return 0;
				}
			}
		}
	}
	
	printf(&#34;0,0&#34;);
	
	return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>